import { config, useAuth } from 'ice';
import React from 'react';

interface IAuthProps {
  children: any;
  authKey: string;
  fallback?: React.ReactNode;
}

export default function Auth(props: IAuthProps) {
  const { children, authKey, fallback } = props;
  const [auth] = useAuth();
  // 判断是否有权限
  const hasAuth = auth[authKey];

  // TODO: 本地开发环境默认放开按钮权限
  if (config.env === 'local') {
    return children;
  }
  // 有权限时直接渲染内容
  if (hasAuth) {
    return children;
  }
  // 无权限时显示指定 UI
  return fallback || null;
}
